﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Reflection;
using System.IO;


namespace Treasures
{
    public class DoubleBufferedPanel : Panel
    {

        #region Fields and Props

        public List<LabelAnimation> LabelAnim { get; set; }
        public AdvButton resetButton1 { get; set; }
        public AdvButton resetButton2 { get; set; }
        public bool Reset1Performed { get; set; }
        public bool Reset2Performed { get; set; }

        public int FPSCounter { get; set; }
        public string FPSText { get; set; }

        public Label tb { get; set; }
        private Bitmap BG { get; set; }

        #endregion

        #region ctor

        public DoubleBufferedPanel()
            : base()
        {
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.DoubleBuffered = true;
            tb = new Label();
            tb.Size = new Size(20, 15);
            tb.Visible = false;
            this.Controls.Add(tb);
            
            resetButton1 = new AdvButton();
            resetButton1.Location = Constants.Reset1Loc;
            resetButton2 = new AdvButton();
            resetButton2.Location = Constants.Reset2Loc;
            LabelAnim = new List<LabelAnimation>();
            BG = new Bitmap(Image.FromFile(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"res\BG\BG.png")));
            FPSCounter = 0;
            FPSText = String.Empty;
        }

        #endregion

        #region Events handling
        protected override void OnPaintBackground(PaintEventArgs pe)
        {
            return;
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            Graphics g;
            Bitmap drawing = null;

            drawing = new Bitmap(this.Width, this.Height, pe.Graphics);
            g = Graphics.FromImage(drawing);
            g.DrawImage(BG, new Point(0, 0));
            if (ElementMap.Instance.ColorMap.Count > 0)
            {
                lock (ElementMap.Instance.ColorMap)
                {
                    lock (ElementMap.Instance.ColorMap.Peek())
                    {
                        for (int i = 0; i < ElementMap.Instance.ColorMap.Peek().GetLength(0); i++)
                        {
                            for (int j = 0; j < ElementMap.Instance.ColorMap.Peek().GetLength(1); j++)
                            {
                                ElementMap.Instance.ColorMap.Peek()[i, j].Chip.DrawElementFigure(g, (int)ElementMap.Instance.PointMap[i, j].X, (int)ElementMap.Instance.PointMap[i, j].Y);
                                if (ElementMap.Instance.ColorMap.Peek()[i, j].ClickedAsSelection == true)
                                {
                                    ElementMap.Instance.SelFigure.DrawElementFigure(g, (int)ElementMap.Instance.PointMap[i, j].X, (int)ElementMap.Instance.PointMap[i, j].Y);
                                }
                            }
                        }
                    }
                }

                if (!Reset1Performed)
                {
                    resetButton1.DrawElementFigure(g, resetButton1.Location.X, resetButton1.Location.Y);
                }
                if (!Reset2Performed)
                {
                    resetButton2.DrawElementFigure(g, resetButton2.Location.X, resetButton2.Location.Y);
                }



                GraphicsPath myPath = new GraphicsPath();
                FontFamily family = new FontFamily("Arial");
                int emSize = 24;
                string stringText = String.Empty;
                Point origin;
                StringFormat format = StringFormat.GenericDefault;
                lock (this.LabelAnim)
                {
                    for (int i = 0; i < this.LabelAnim.Count; i++)
                    {
                        // Set up all the string parameters.
                        stringText = LabelAnim[i].Text;

                        origin = LabelAnim[i].Location;

                        // Add the string to the path.
                        myPath.AddString(stringText,
                            family,
                            (int)FontStyle.Regular,
                            emSize,
                            origin,
                            format);

                        //Draw the path to the screen.
                        g.FillPath(new SolidBrush(Color.FromArgb(LabelAnim[i].Opacity, Color.LightYellow)), myPath);
                        g.DrawPath(new Pen(new SolidBrush(Color.FromArgb(LabelAnim[i].Opacity, Color.Yellow)), 2), myPath);
                        }
                }
                //other DrawStrings (point, lvls)
                myPath = new GraphicsPath();
                emSize = 30;
                stringText = ElementMap.Instance.LControl.PointScore.ToString();
                origin = new Point(820, 100);
                myPath.AddString(stringText,
                    family,
                    (int)FontStyle.Regular,
                    emSize,
                    origin,
                    format);
                g.FillPath(new SolidBrush(Color.FromArgb(218, 166, 106)), myPath);
                g.DrawPath(new Pen(new SolidBrush(Color.FromArgb(169, 135, 44)), 1), myPath);

                myPath = new GraphicsPath();
                emSize = 55;
                stringText = ElementMap.Instance.LControl.Level.ToString();
                origin = new Point(750, 400);
                myPath.AddString(String.Format("Level {0}", stringText),
                    family,
                    (int)FontStyle.Regular,
                    emSize,
                    origin,
                    format);
                g.FillPath(new SolidBrush(Color.FromArgb(218, 166, 106)), myPath);
                g.DrawPath(new Pen(new SolidBrush(Color.FromArgb(169, 135, 44)), 2), myPath);
            }
            tb.Text = FPSText;
            if (drawing == null)
            {
                throw new NullReferenceException("drawing == null");
            }
            pe.Graphics.DrawImageUnscaled(drawing, 0, 0);
            g.Dispose();
        }

        #endregion
    }
}